﻿using System;
using System.Reflection;
using Newtonsoft.Json;
using woaPoker.LogicLib.LogicCtrl;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
using woaC2.SocketLib;
using woaC2.LogicLib.DataContract.Input;
namespace woaC2.LogicLib
{
    
    public class Service
    {
        #region Call
        public static string Call(Guid sessionId, string methodName, string parObj)
        {
            Type calledType = Type.GetType("woaC2.LogicLib.Service");

            String val = (String)calledType.InvokeMember(
                            methodName,
                            BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Static,
                            null,
                            null,
                            new Object[] { sessionId, parObj });
            //SocketServer.Instance.pro
            return val;
            

        }
        #endregion

        
        //-----------------------------------------------------------------//
        //  Toàn bộ các hàm service của client gọi sẽ được invoke tại đây  //
        //-----------------------------------------------------------------//

        private static string DangNhap(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<DangNhapInDC>(jsonIn);
            var objOut = (new LoginGameLgCtrl()).DangNhap(sessionId, objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "DangNhap$" + jsonOut;
        }

        private static string GetListRoom(Guid sessionId,string jsonIn)
        {
            var objOut = (new LoginGameLgCtrl()).GetListRoom();
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "GetListRoom$" + jsonOut;
        }

        private static string CreateRoom(Guid sessonId, string jsonIn)
        {
            var objIn=JsonConvert.DeserializeObject<CreateRoomInDC>(jsonIn);
            var objOut = (new CreateGameLgCtrl()).CreateRoom(sessonId, objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "CreateRoom$" + jsonOut;
        }

        private static string LoginGuest(Guid sessonId,string jsonIn)
        {
            var objOut = (new LoginGameLgCtrl()).LoginGuest(sessonId);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "LoginGuest$" + jsonOut;
        }
        #region ducon
        private static string StartGame(Guid sessionId, string jsonIn)
        {
            //var objIn = JsonConvert.DeserializeObject<MatchInDc>(jsonIn);
            var objOut = (new StartGameLgCtrl()).StartGame(sessionId);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "GameStart$" + jsonOut;
        }
        private static string GameCancel(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<MatchInDc>(jsonIn);
            var objOut = (new WaitRoomGameLgCtrl()).GameCancel(sessionId,objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "GameCancel$" + jsonOut;
        }
        private static string RemovePlayer(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<PlayerInDC>(jsonIn);
            var objOut = (new WaitRoomGameLgCtrl()).RemovePlayer(sessionId, objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "RemovePlayer$" + jsonOut;
        }
        private static string ChangeTeam(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<MatchInDc>(jsonIn);
            var objOut = (new WaitRoomGameLgCtrl()).ChangeTeam(sessionId,objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "ChangeTeam$" + jsonOut;
        }
        private static string ListPlayer(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<MatchInDc>(jsonIn);
            var objOut = (new WaitRoomGameLgCtrl()).ListPlayer(sessionId,objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "ListPlayer$" + jsonOut;
        }
        #endregion
        private static string RemoveRoom(Guid sessionID,string jsonIn)
        {
            var objOut = MathRegistry.instance.Remove(sessionID);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "RemoveRoom$" + jsonOut;

        }

        private static string Shoot(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<ShootInDc>(jsonIn);
            var objOut = (new StartGameLgCtrl()).Shoot(objIn, sessionId);
            var jsonOut=JsonConvert.SerializeObject(objOut);
            return "Shoot$" + jsonOut;

        }

        private static string Move(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<MoveInDc>(jsonIn);
            var objOut = (new StartGameLgCtrl()).Move(objIn, sessionId);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "Move$" + jsonOut;
        }

        private static string JoinGame(Guid sessionId, string jsonIn)
        {
            var objIn = JsonConvert.DeserializeObject<JoinGameInDC>(jsonIn);
            var objOut = (new CreateGameLgCtrl()).JoinGame(sessionId,objIn);
            var jsonOut = JsonConvert.SerializeObject(objOut);
            return "JoinGame$" + jsonOut;
        }
        //private static string StartGame(Guid sessonId, string jsonIn)
        //{
        //    var objIn = JsonConvert.DeserializeObject<MatchInDc>(jsonIn);
        //    var objOut = (new CreateGameLgCtrl()).StartGame(objIn);
        //    var jsonOut = JsonConvert.SerializeObject(objOut);
        //    return "StartGame$" + jsonOut;
        //}
        //private static string Guest()
        //{

        //}
        //private static string CreatMatch(Guid sessionId)
        //{
        //    Match match = new Match(PlayerRegistry.instance.GetPlayer(sessionId), 1);
        //}

        ////List<Match> li = new List<Match>();       
        //private static string Register(Guid sessionId, string jsonIn)
        //{

        //    var objIn = JsonConvert.DeserializeObject<RegisterInDc>(jsonIn);

        //    var obj = (new LoginGame()).dangki();

        //    var objOut = (new LoginGameLgCtrl()).Register(sessionId, objIn);
        //    bool a = true;

        //    var jsonOut = JsonConvert.SerializeObject(a);

        //    return "Register$" + jsonOut;
        //}

        //private static string DangNhap(Guid sessionId, string jsonIn)
        //{
        //    var objIn = JsonConvert.DeserializeObject<DangNhapInDC>(jsonIn);
        //    var objOut=
        //}

    }
}
